<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="naogen" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addID(curID)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :default-expand-all="true"
          :data="list"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="naogen" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addID(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item :is-edit="isEdit" @click.native="EdieID(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="delID(data.id) ">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

        <el-dialog

          :title="isEdit===true ?'编辑' : '新增' "
          :visible.sync="showdialog"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <departdialog v-if="showdialog " :id="curID" :originlist="originlist" :is-edit="isEdit" @Hshow="Hshoww" />
        </el-dialog>
      </el-card>

    </div>
  </div>
</template>
<script>
import { trenDatatoTree } from '@/utils/index'
import { getbumenlist, delllist } from '@/api/department'
import departdialog from './departdialog.vue'
export default {
  components: {
    departdialog
  },
  data() {
    return {
      originlist: [], // 包含属性的新数组
      isEdit: false,
      curID: '',
      // 依赖一份树形数据
      list: [],
      showdialog: false
    }
  },
  created() {
    this.getblist()
  },
  methods: {
    async getblist() {
      try {
        const res = await getbumenlist()
        console.log(res, 'getbumenlist')
        // 去除第一个元素 shift 方法
        // 便利出新数组里面有我们需要的属性
        this.originlist = res.data.depts.map((item) => {
          return { id: item.id, name: item.name, pid: item.pid, code: item.code }
        })
        console.log(this.originlist)
        res.data.depts.shift()
        this.list = trenDatatoTree(res.data.depts)
      } catch (error) {
        //
      }
    },
    addID(id) {
      this.curID = id
      this.showdialog = true
      this.isEdit = false
    },
    Hshoww() {
      this.getblist()
      this.showdialog = false
    },
    EdieID(id) {
      this.curID = id
      this.isEdit = true // 变为编辑模式
      this.showdialog = true
    },
    delID(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delllist(id)
        this.getblist()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
